package net.daum.webtoon.android;

import java.util.ArrayList;

import net.daum.webtoon.android.jibx.data.FileManager;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class WebToonListActivity extends Activity implements OnMenuItemClickListener {

	public static String TAG = WebToonListActivity.class.getSimpleName();
	private ListView mView;
	private final String TOONRSS = "http://cartoon.media.daum.net/webtoon/rss/";
	private final String TOONDOMAIN = "http://cartoon.media.daum.net/";
	public static String MODE = "VIEW";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list);
		Log.d(TAG, MainActivity.gInfo.size() + "");
		mView = (ListView) findViewById(R.id.main_list);
		MainActivity.gInfo = MainActivity.getGInfo(getApplicationContext());
		if (!getIntent().getBooleanExtra("ISSVAED", false))
			FileManager.saveObj(getApplicationContext(), MainActivity.gInfo, FileManager.WEEKLISTPATH, FileManager.makeFileNameByDate() + ".obj");

		mView.setAdapter(new WebToonListAdapter(getApplicationContext(), MainActivity.gInfo));

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		mView = (ListView) findViewById(R.id.main_list);
		
		MainActivity.gInfo = MainActivity.getGInfo(getApplicationContext());
		
		if (!getIntent().getBooleanExtra("ISSVAED", false))
			FileManager.saveObj(getApplicationContext(), MainActivity.gInfo, FileManager.WEEKLISTPATH, FileManager.makeFileNameByDate() + ".obj");

		mView.setAdapter(new WebToonListAdapter(getApplicationContext(), MainActivity.gInfo));
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);

		menu.getItem(0).setTitle(MODE.trim());
		menu.getItem(0).setOnMenuItemClickListener(this);
		return true;
	}

	private class WebToonListAdapter extends ArrayAdapter<WebToonInfo> {

		public WebToonListAdapter(Context context, ArrayList<WebToonInfo> aLis) {
			super(context, R.layout.info_item, aLis);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			ViewWrapper wrapper = null;

			if (row == null) {
				row = getLayoutInflater().inflate(R.layout.info_item, null);
				wrapper = new ViewWrapper(row);
				row.setTag(wrapper);
			} else
				wrapper = (ViewWrapper) row.getTag();

			final WebToonInfo info = MainActivity.gInfo.get(position);
			wrapper.getBase().setBackgroundColor((position % 2) == 0 ? android.R.color.background_dark : android.R.color.background_light);
			wrapper.getTitle().setText(info.title);
			wrapper.getWriter().setText(info.writer);
//			Log.d(TAG, "info.printText() == " + position + "");

			final int idx = position;
			row.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					startActivity(new Intent(WebToonListActivity.this, ContentListActivity.class).putExtra(ContentListActivity.TOONNAME, info.eng_id));
				}
			});
			return row;
		}

		private class ViewWrapper {
			private View mmmBase;
			private TextView mmmtitle;
			private TextView mmmwriter;

			public ViewWrapper(View base) {
				mmmBase = base;
			}

			public View getBase() {
				return mmmBase;
			}

			public TextView getTitle() {
				if (mmmtitle == null) {
					mmmtitle = (TextView) mmmBase.findViewById(R.id.title);
					mmmtitle.setSelected(true);
				}
				return mmmtitle;
			}

			public TextView getWriter() {
				if (mmmwriter == null) {
					mmmwriter = (TextView) mmmBase.findViewById(R.id.writer);
					mmmwriter.setSelected(true);
				}
				return mmmwriter;
			}
		}

	}

	@Override
	public boolean onMenuItemClick(MenuItem item) {
		// TODO Auto-generated method stub
//		switch (item.getItemId()) {
//			case 0:
		if (MODE.equalsIgnoreCase(getString(R.string.menu_save).trim())) {
			MODE = getString(R.string.menu_view).trim();
		} else {
			MODE = getString(R.string.menu_save).trim();

		}

//		}
		item.setTitle(MODE);

		return false;
	}

}
